package com.jsecode.protocol.gid22.entity;

import com.jsecode.protocol.gid22.GidBody;

import io.netty.buffer.ByteBuf;

public class GidDSFlag extends GidBody {
    public byte[] dsMask;  // assert is should be 5
    
    public GidDSFlag(byte[] dsMask) {
        assert(dsMask.length ==5);
        this.dsMask = dsMask;
    }
    
    public boolean isMIStatus() {
        return isBitOne(dsMask[0], 7);
    }
    public boolean isDTCEngine() {
        return isBitOne(dsMask[0], 6);
    }
    public boolean isOBDType() {
        return isBitOne(dsMask[0], 5);
    }
    public boolean isVoltage() {
        return isBitOne(dsMask[0], 4);
    }
    public boolean isRPM() {
        return isBitOne(dsMask[0], 3);
    }
    public boolean isVehiclSpeed() {
        return isBitOne(dsMask[0], 2);
    }
    public boolean isIntakeTemp() {
        return isBitOne(dsMask[0], 1);
    }
    public boolean isCoolTemp() {
        return isBitOne(dsMask[0], 0);
    }
    
    public boolean isEnTemp() {
        return isBitOne(dsMask[1], 7);
    }
    public boolean isIntakePres() {
        return isBitOne(dsMask[1], 6);
    }
    public boolean isFuelPres() {
        return isBitOne(dsMask[1], 5);
    }
    public boolean isBaroPres() {
        return isBitOne(dsMask[1], 4);
    }
    public boolean isAirFlowSensor() {
        return isBitOne(dsMask[1], 3);
    }
    public boolean isThrotleSensor() {
        return isBitOne(dsMask[1], 2);
    }
    public boolean isPedalPos() {
        return isBitOne(dsMask[1], 1);
    }
    public boolean isEngineRunTime() {
        return isBitOne(dsMask[1], 0);
    }
    
    public boolean isFaultMileage() {
        return isBitOne(dsMask[2], 7);
    }
    public boolean isOilMass() {
        return isBitOne(dsMask[2], 6);
    }
    public boolean isEngineLoad() {
        return isBitOne(dsMask[2], 5);
    }
    public boolean isLongFuelTrim() {
        return isBitOne(dsMask[2], 4);
    }
    public boolean isSparkAngle() {
        return isBitOne(dsMask[2], 3);
    }

    public boolean isPanelMileage(){
        return isBitOne(dsMask[2], 2);
    }
    // TODO: no isAMT ...
//    public boolean isAMT() {
//        return isBitOne(dsMask[2], 2);
//    }
//    public boolean isDampOil() {
//        return isBitOne(dsMask[2], 1);
//    }
//    public boolean isInstantOil() {
//        return isBitOne(dsMask[2], 0);
//    }
    
    public boolean isBrakeLevel() {
        return isBitOne(dsMask[3], 7);
    }
    
    public boolean isTireLF() {
        return isBitOne(dsMask[4], 6);
    }
    public boolean isTireRF() {
        return isBitOne(dsMask[4], 5);
    }
    public boolean isTireLB() {
        return isBitOne(dsMask[4], 4);
    }
    public boolean isTireRB() {
        return isBitOne(dsMask[4], 3);
    }
    public boolean isMileageType() {
        return isBitOne(dsMask[4], 2);
    }
    public boolean isMileage() {
        return isBitOne(dsMask[4], 1);
    }
    public boolean isTotalFuelConsumption() {
        return isBitOne(dsMask[4], 0);
    }

    // byte[0]
    public int miStatus=-1; // 故障码灯状态(MIL) if(Bit0) ON else     OFF
    public int dtcEngine=-1; // u8         发动机故障码个数
    public int obdType=-1; // OBD类型 
                            //           =0x01 OBD II
                            //            =0x02 OBD
                            //            =0x03 OBD and OBD II
                            //            =0x04 OBD I
                            //            =0x05 NO OBD
                            //            =0x06 EOBD
                            //            =0x07 EOBD and OBD II
                            //            =0x08 EOBD and OBD
                            //            =0x09 EOBD,OBD and OBD II
                            //            =0x0A JOBD
                            //            =0x0B JOBD and OBD II
                            //            =0x0C JOBD and EOBD
                            //            =0x0D JOBD,EOBD,and OBD II
                            //            =0x0E EURO IV B1
                            //            =0x0F EURO V B2
                            //            =0x10 EURO C
                            //            =0x11 EMD
                            //            =0x12 SAE J1939
                            //            其他  ---

    public float voltage; // 0.1  V   电瓶电压显示值为上传值/10
    public int rpm=-1; // u16      RPM 发动机转速
    public int vehiclSpeed=-1; // u8       Km/h    车辆速度显示值为上传值
    public int intakeTemp=-100; // 进气口温度        (上传范围0~255) 显示值为上传值-40 (实际范围-40~215)
    public int coolTemp=-100; // 水箱温度           (上传范围0~255)    显示值为上传值-40  (实际范围-40~215)

    // byte[1]
    public int enTemp=-100; // 车辆环境温度       (上传范围0~255) 显示值为上传值-40  (实际范围-40~215
    public int intakePres=-1; // kpa 进气歧管压力  10~105kpa
    public int fuelPres=-1; // kPa 燃油压力
    public int baroPres=-1; // kPa 大气压力
    public float airFlowSensor=-1; // 0.1 g/s 空气流量 显示值为上传值/10
    public float throtleSensor=-1; // 0.1 %   绝对节气门位置传感器 显示值为上传值/10
    public float pedalPos=-1; // 0.1 %   油门踏板位置， 0~100 显示值为上传值/10
    public int engineRunTime=-1; // Sec 发动机启动后运行时间
    
    // byte[2]
    public long faultMileage=-1; // km  故障行驶里程
    public float oilMass=-1; // 0.1 L/% 剩余油量，单位L或%Bit15 ==0百分比%  OBD都为百分比      ==1单位L 显示值为上传值/10
    public int engineLoad=-1; //    %   发动机负荷，   0~100
    public float longFuelTrim=-1; // 0.1 %   长期燃油修正(组1) 显示值为上传值/10
    public float sparkAngle=-1; // 0.1 °   点火提前角 显示值为(上传值/10)-64
    public long panelMileage = -1; // 仪表盘里程，单位米
//    public int amt=-1;
//    public int dampOil=-1;
//    public int instantOil=-1;
    
    // byte[3]
    public int brakeLevel=-1;

    // byte[4]
    public int tireLF=-1; // Tire Pressue LF 2   u16     Kpa 左前胎压
    public int tireRF=-1; // Tire Pressue RF 2   u16     Kpa 右前胎压
    public int tireLB=-1; // Tire Pressue LB 2   u16     Kpa 左后胎压
    public int tireRB=-1; // Tire Pressue RB 2   u16     Kpa 右后胎压
    public int mileageType=-1; // Mileage & Fuel Flag 1   u8          总里程类型Bit1,Bit0两位
                // 1 汽车里程
                // 2 OBD里程
                // 3 GPS里程
    public long mileage=-1l; // Total Mileage   4   u32 0.001   Km  行驶里程,上传值单位为m(米)
    public long totalFuelConsumption=-1l; // Total Fuel Consumption  4   u32 0.001   L   总的燃油消耗量,上传值单位为ml(毫升)

    @Override
    public int unpack(ByteBuf srcBuf) {
        // byte[0]
        if( isMIStatus()) {
            miStatus = srcBuf.readUnsignedByte();
        }
        if( isDTCEngine()) {
            dtcEngine = srcBuf.readUnsignedByte();
        }
        if( isOBDType()) {
            obdType = srcBuf.readUnsignedByte();
        }
        if(isVoltage()) {
            voltage = srcBuf.readUnsignedShort() / 10f;
        }
        if(isRPM()) {
             rpm = srcBuf.readUnsignedShort();
        }
        if(isVehiclSpeed()) {
            vehiclSpeed = srcBuf.readUnsignedByte();
        }       
        if(isIntakeTemp()) {
            intakeTemp = srcBuf.readUnsignedByte();
        }
        if(isCoolTemp()) {
            coolTemp = srcBuf.readUnsignedByte() - 40;
        }
        // byte[1]
        if(isEnTemp()) {
            enTemp = srcBuf.readUnsignedByte() - 40;
        }
        if(isIntakePres()) {
            intakePres = srcBuf.readUnsignedByte();
        }
        if( isFuelPres() ) {
            fuelPres = srcBuf.readUnsignedShort();
        }
        if( isBaroPres() ) {
            baroPres = srcBuf.readUnsignedByte();
        }
        if( isAirFlowSensor()) {
            airFlowSensor = srcBuf.readUnsignedShort() / 10f;
        }
        if( isThrotleSensor()) {
            throtleSensor = srcBuf.readUnsignedShort() / 10f;
        }
        if( isPedalPos()) {
            pedalPos = srcBuf.readUnsignedShort() / 10f;
        }
        if(isEngineRunTime()) {
            engineRunTime = srcBuf.readUnsignedShort();
        }
        // byte[2]
        if(isFaultMileage()) {
            faultMileage = srcBuf.readUnsignedInt();
        }        
        if(isOilMass()) {
            oilMass = srcBuf.readUnsignedShort() / 10f;
        }
        if(isEngineLoad()) {
            engineLoad = srcBuf.readUnsignedByte();
        }
        if( isLongFuelTrim() ) {
            longFuelTrim = srcBuf.readUnsignedShort()/10;
        }
        if( isSparkAngle() ) {
            sparkAngle = srcBuf.readUnsignedShort() / 10f - 64;
        }
        
        // 2015.7.20新加入
        if (isPanelMileage() ) {
            panelMileage = srcBuf.readUnsignedInt();
        }
//        if( isAMT()) {
//            amt = srcBuf.readUnsignedShort();
//        }
//        if( isDampOil() ) {
//            dampOil = srcBuf.readUnsignedByte();
//        }
//        if( isInstantOil() ) {
//            instantOil = srcBuf.readUnsignedByte();
//        }

        // byte[3]
        if( isBrakeLevel() ) {
            brakeLevel = srcBuf.readUnsignedByte();
        }

        // byte[4]
        if(isTireLF()) {
            tireLF = srcBuf.readUnsignedShort();
        }
        if(isTireRF()) {
            tireRF = srcBuf.readUnsignedShort();
        }
        if(isTireLB()) {
            tireLB = srcBuf.readUnsignedShort();
        }
        if(isTireRB()) {
            tireRB = srcBuf.readUnsignedShort();
        }
        if(isMileageType()) {
            mileageType = srcBuf.readUnsignedByte();
        }
        if(isMileage()) {
            mileage = srcBuf.readUnsignedInt();
        }
        if(isTotalFuelConsumption()) {
            totalFuelConsumption = srcBuf.readUnsignedInt();
        }
        return 0;
    }
    
    @Override
    public String toString(){
        StringBuilder sb = new StringBuilder();

        // byte[0]
        sb.append("miStatus=").append(miStatus).append(",");
        sb.append("dtcEngine=").append(dtcEngine).append(",");
        sb.append("obdType=").append(obdType).append(",");
        sb.append("voltage=").append(voltage).append(",");
        sb.append("rpm=").append(rpm).append(",");
        sb.append("vehiclSpeed=").append(vehiclSpeed).append(",");
        sb.append("intakeTemp=").append(intakeTemp).append(",");
        sb.append("coolTemp=").append(coolTemp).append(",");
        
        // byte[1]
        sb.append("enTemp=").append(enTemp).append(",");
        sb.append("intakePres=").append(intakePres).append(",");
        sb.append("fuelPres=").append(fuelPres).append(",");
        sb.append("baroPres=").append(baroPres).append(",");
        sb.append("airFlowSensor=").append(airFlowSensor).append(",");
        sb.append("throtleSensor=").append(throtleSensor).append(",");
        sb.append("pedalPos=").append(pedalPos).append(",");
        sb.append("engineRunTime=").append(engineRunTime).append(",");

        // byte[2]
        sb.append("faultMileage=").append(faultMileage).append(",");
        sb.append("oilMass=").append(oilMass).append(",");
        sb.append("engineLoad=").append(engineLoad).append(",");
        sb.append("longFuelTrim=").append(longFuelTrim).append(",");
        sb.append("sparkAngle=").append(sparkAngle).append(",");
        // NOTE: 2015.7.20新加, panelMileage, 其他的三项协议中没有
        sb.append("panelMileage=").append(panelMileage).append(",");
//        sb.append("amt=").append(amt).append(",");
//        sb.append("dampOil=").append(dampOil).append(",");
//        sb.append("instantOil=").append(instantOil).append(",");
        
        // byte[3]
        sb.append("brakeLevel=").append(brakeLevel).append(",");
        
        // byte[4]
        sb.append("tireLF=").append(tireLF).append(",");
        sb.append("tireRF=").append(tireRF).append(",");
        sb.append("tireLB=").append(tireLB).append(",");
        sb.append("tireRB=").append(tireRB).append(",");
        sb.append("mileageType=").append(mileageType).append(",");
        sb.append("mileage=").append(mileage).append(",");
        sb.append("totalFuelConsumption=").append(totalFuelConsumption);
        
        return sb.toString();
    }
    
    /**
     * @param uByte 
     * @param bitIndex 表示bit0, bit1,...
     *
     */
    private static boolean isBitOne(byte uByte, int bitIndex) {
        return ((uByte&(0x1<<bitIndex))>0);
    }
    

}
